<?php

// 编辑创业资讯
// PUT information.admin.edit

namespace app\api\resource\information\admin;
use think\Db;
use think\Loader;

class Edit
{
    // 方法
    public $_method = ['PUT'];
    // 前置
    public $_pre    = ['adminLogin'];
    // 描述
    public $_description = '编辑创业资讯';
    // 参数
    public $_param  = [
        'id'      => '资讯主键',
        'title'   => '标题',
        'cover'   => '封面',
        'content' => '内容',
    ];
    // 返回
    public $_return = [
       201 => '编辑成功',
       422 => '请求参数不合法',
       500 => '服务器异常',
    ];

    public function run(&$request)
    {
        $params = $request->put();

        $validate = Loader::validate('Information');
        if ( !$validate->scene('edit')->check($params) ) {
            $errMsg = $validate->getError();
            return [422,$errMsg];
        }

        $params['reading'] = 0;
        $params['create_time'] = time();

        $num = Db::name('information')->where('id',$params['id'])->update($params);

        return ($num > 0) ? [ 201,[] ] : 500;
    }
}